package net;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Observable;

import util.DeviceInfo;
import ComLayer.ComunicationLayerInterface;

/**
 * Class NetComunicationLayer
 */
public class NetComunicationLayer extends java.util.Observable implements
		ComunicationLayerInterface {

	int status = 0; // (0 inactivo 1 activo)
	NetReaderThread server;
	java.util.LinkedList<String> actionlist;
	java.util.LinkedList<DeviceInfo> devicelist;

	private String ip;
	private int port = 17745;
	private String hostname;

	public NetComunicationLayer(int portc) {

		if (port != 0)
			port = portc;
		server = new NetReaderThread(port, this);
		actionlist = new LinkedList<String>();
		devicelist = new LinkedList<DeviceInfo>();
	};


	/**
	 * @return int
	 */
	public int sendText(String text) {
		int ret = 0;
		try {
			ret = sendStringText(text);
		} catch (IOException e) {
			ret = 2;
		}
		return ret;
	}

	@Override
	public DeviceInfo getDeviceInfo() {
		// TODO Auto-generated method stub
		DeviceInfo device = new DeviceInfo(hostname, ip + ":" + port);
		return device;
	}

	private int sendStringText(String mensaje) throws IOException {
		Socket socketclient = null;
		int ret = 0;
		try {
			socketclient = new Socket(ip, port);
		} catch (IOException e) {
			System.out
					.println("Ip incorrecta o el host al que desea acceder carece de este servicio");
			ret = 2;
		}
		DataOutputStream salidaserver = new DataOutputStream(socketclient
				.getOutputStream());
		BufferedReader entradaserver = new BufferedReader(
				new InputStreamReader(socketclient.getInputStream()));
		String chivato = "false";

		while (!chivato.equals("ACK")) {
			salidaserver.writeBytes(mensaje + "\n");
			salidaserver.writeBytes("FIN\n");
			chivato = entradaserver.readLine();
		}
		System.out.println("Mensaje: " + mensaje);
		System.out.println("Entregado en "
				+ socketclient.getInetAddress().getHostName());
		hostname = socketclient.getInetAddress().getHostName();
		salidaserver.close();
		entradaserver.close();
		socketclient.close();
		return ret;

	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getHostname() {
		return hostname;
	}

	@Override
	/*
	 * (non-Javadoc)
	 *
	 * @see ComLayer.ComLayerReaderInterface#addActionComand(java.lang.String)
	 */
	public void addActionComand(DeviceInfo device, String text) {
		actionlist.add(text);
		devicelist.add(device);
		this.setChanged();
		this.notifyObservers();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ComLayer.ComLayerReaderInterface#getListaacciones()
	 */
	public java.util.LinkedList<String> getListaacciones() {
		return actionlist;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ComLayer.ComLayerReaderInterface#getStatus()
	 */
	public int getStatus() {
		return status;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ComLayer.ComLayerReaderInterface#setStatus(int)
	 */
	public void setStatus(int statuss) {
		this.status = statuss;
		if (statuss == 0 && (this.status != 0)) {
			this.status = statuss;
			server.interrupt();
		}
		if (statuss == 1) {
			this.status = statuss;
			server.start();
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}

	public Observable getObservable() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public LinkedList<DeviceInfo> getDeviceList() {
		// TODO Auto-generated method stub
		return devicelist;
	}

}
